package other;

import java.util.Random;

/**
 * 洗牌算法的一种实现，
 * 从最后一位往前遍历，遍历到第N位时，随机选择前N-1位的一个元素与第N位的元素交换
 *
 * @author zj
 */
public class Shuffle {
    public static void main(String args[]) {
        int poker[] = new int[54]; //54张扑克
        for (int i = 0; i < poker.length; i++) { //初始化每张扑克
            poker[i] = i;
        }
        shuffle(poker);
    }

    public static void shuffle(int[] poker) {
        if (poker == null || poker.length == 0) {
            return;
        }
        for (int i = poker.length - 1; i > 0; i--) {
            int random = new Random().nextInt(i + 1);
            System.out.print(random + " ");
            int temp = poker[random];
            poker[random] = poker[i];
            poker[i] = temp;
        }
        System.out.println();
        for (int i = 0; i < poker.length; i++) {
            System.out.print(poker[i] + " ");
            if ((i + 1) % 10 == 0) {
                System.out.println();
            }
        }
    }
}
